package ch.bfh.abcmmh.view.main;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import ch.bfh.abcmmh.controller.operating.LogicController;
import ch.bfh.abcmmh.view.GUIController;
import ch.bfh.abcmmh.view.controlview.history.HistoryFrame;
import ch.bfh.abcmmh.view.controlview.history.subpanels.HistoryTab;
import ch.bfh.abcmmh.view.detailview.streets.StreetPanel;
import ch.bfh.abcmmh.view.fullview.FullviewPanel;
import ch.bfh.abcmmh.view.main.simulationparameterframe.ParameterFrame;

public class MainFrame extends JFrame {

	private static final long serialVersionUID = 1L;

	private GUIController gctrl;
	private StreetPanel streetPanel;
	private FullviewPanel fullviewPanel;
	private HistoryFrame historyFrame;

	public MainFrame(LogicController lctrl, GUIController gctrl, JPanel control) {
		this.gctrl = gctrl;
		this.setTitle("TrafficSim");
		this.setSize(gctrl.getMainFrameDimension());
		this.setLocation(gctrl.getMainFrameLocation());
		this.setResizable(false);
		// layering
		this.setLayout(new BorderLayout());
		this.add(control, BorderLayout.NORTH);
		this.streetPanel = new StreetPanel(gctrl);
		this.add(streetPanel, BorderLayout.CENTER);
		this.fullviewPanel = new FullviewPanel(gctrl);
		this.add(this.fullviewPanel, BorderLayout.SOUTH);

		genMenuBar();
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	public StreetPanel getStreetPanel() {
		return streetPanel;
	}

	public HistoryFrame getHistoryFrame() {
		return historyFrame;
	}

	public void setHistoryFrame(HistoryFrame historyFrame) {
		this.historyFrame = historyFrame;
	}

	public FullviewPanel getFullviewPanel() {
		return fullviewPanel;
	}

	private void genMenuBar() {
		JMenuBar menu = new JMenuBar();

		JMenu f_menu = new JMenu("File");
		menu.add(f_menu);

		JMenuItem exit = new JMenuItem("Exit");
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		f_menu.add(exit);

		JMenu o_menu = new JMenu("Options");
		menu.add(o_menu);
		JMenuItem params = new JMenuItem("Simulation parameters");
		params.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new ParameterFrame(gctrl, "Change Simulation Parameter");
			}
		});
		o_menu.add(params);

		JMenuItem history = new JMenuItem("History graph");
		history.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (historyFrame == null) {					
				historyFrame = new HistoryFrame(gctrl, "TS - History");
				gctrl.getLogicController().addObservertoClock(historyFrame);
				}
			}
		});
		o_menu.add(history);

		JMenu h_menu = new JMenu("Help");
		menu.add(h_menu);
		JMenuItem docu = new JMenuItem("Documentation");
		docu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Open documentation
			}
		});
		h_menu.add(docu);

		this.setJMenuBar(menu);
	}

	public void positionChanged() {
		this.streetPanel.getStreetController().positionChanged();
	}
	
	public HistoryTab getShowingHistoryTab() {
		return historyFrame.getShowingPanel();
	}
}